package client.GUI;

import javax.swing.JFrame;

import client.GUI.dashboard.DashboardPanel;
import client.GUI.login.LoginPanel;
import client.GUI.order.NewOrderPanel;
import client.GUI.login.LostPasswordPanel;
import client.GUI.order.LimitOrder;
import client.GUI.order.TrailingStop;
import client.backend.price.Price;
import client.backend.price.PriceController;

import java.awt.Dimension;
import java.awt.Container;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

/**
 * Establishes the GUI for the client
 *
 */
public class GUIClient extends JFrame {

	private Container pane = this.getContentPane();
	private static GUIClient instance = null;
	
	private JPanel topPanel = new JPanel();
	private BottomPanel bottomPanel = new BottomPanel();
	
	private LoginPanel loginPanel = LoginPanel.getInstance();
	private LostPasswordPanel lostPasswordPanel = LostPasswordPanel.getInstance();
	private DashboardPanel dashboardPanel = DashboardPanel.getInstance();
	private NewOrderPanel newOrderPanel;
	private LimitOrder limitOrder;
	private TrailingStop trailingStop;
	
	/**
	 * This is the default constructor
	 * Sets all the dimensions, adds all the panels
	 */
	private GUIClient() {
		super();
		this.setSize(1024, 768);
		this.setPreferredSize(new Dimension(1024, 768));
		this.setMinimumSize(new Dimension(1024, 768));
		this.setMaximumSize(new Dimension(1024, 768));
		this.setResizable(false);
		this.setTitle("Automated Currency Exchange");
		this.setLayout(null);
		
		topPanel.setLayout(null);
		topPanel.setSize(1024, 700);
		
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		bottomPanel.setBounds(0, 700, 1024, 50);
		
		pane.add(topPanel);
		pane.add(bottomPanel);
		pane.add(loginPanel);
		pane.add(lostPasswordPanel);
		
		topPanel.setVisible(false);
		bottomPanel.setVisible(false);
		
		topPanel.add(dashboardPanel);
		
		displayLogin();
	}
	
	/**
	 * Checks for existing instances of GUIClient
	 */
	public static GUIClient getInstance() {
		if (instance == null) {
			instance = new GUIClient();
		}
		return instance;
	}
	
	/**
	 * Displays the login panel
	 */
	public void displayLogin() {
		showLoginPanel();
		hidePanels();
		loginPanel.setVisible(true);
	}
	
	/**
	 * Sets up the dashboard panel and hides the login
	 * panels once the user is logged in
	 */
	public void loggedIn() {
		hidePanels();
		showDisplayPanels();
		dashboardPanel.initialize();
		dashboardPanel.setVisible(true);
	}
	
	/**
	 * Takes the user back to the dashboard from the new order panel
	 */
	public void backFromNewOrder() {
		hidePanels();
		showDisplayPanels();
		dashboardPanel.setVisible(true);
	}
	
	/**
	 * Takes the user back to the new order panel from the limit order panel
	 */
	public void backFromLimitOrder() {
		hidePanels();
		showDisplayPanels();
		newOrderPanel.setVisible(true);
	}
	
	/**
	 * Takes the user back to the new order panel from the trailing stop panel
	 */
	public void backFromTrailingStop() {
		hidePanels();
		showDisplayPanels();
		newOrderPanel.setVisible(true);
	}
	
	/**
	 * Displays the forgot password screen
	 * 
	 * @param username	- The String the holds the client's username
	 */
	public void forgotPassword(String username) {
		showLostPasswordPanel();
		
		lostPasswordPanel.setFields(username);
	}
	
	/**
	 * Shows the login panel
	 */
	public void showLoginPanel() {
		loginPanel.setVisible(true);
		topPanel.setVisible(false);
		bottomPanel.setVisible(false);
		lostPasswordPanel.setVisible(false);
	}
	
	/**
	 * Shows the beginning panels
	 */
	public void showDisplayPanels() {
		topPanel.setVisible(true);
		bottomPanel.setVisible(true);
		loginPanel.setVisible(false);
		lostPasswordPanel.setVisible(false);
	}
	
	/**
	 * Shows the lost password panel
	 */
	public void showLostPasswordPanel() {
		loginPanel.setVisible(false);
		topPanel.setVisible(false);
		bottomPanel.setVisible(false);
		lostPasswordPanel.setVisible(true);
	}
	
	/**
	 * Hides the panels
	 */
	public void hidePanels() {
		lostPasswordPanel.setVisible(false);
		dashboardPanel.setVisible(false);
		
		if (newOrderPanel != null)
			newOrderPanel.setVisible(false);
		if (limitOrder != null)
			limitOrder.setVisible(false);
		if (trailingStop != null)
			trailingStop.setVisible(false);
	}
	
	/**
	 * Creates a new order
	 * 
	 * @param curr1		The currency pair chosen by the user
	 */
	public void newOrder(String curr1) {
		hidePanels();
		newOrderPanel = new NewOrderPanel(curr1);
		topPanel.add(newOrderPanel);
		newOrderPanel.setVisible(true);
	}

	/**
	 * Create a new limit order for the user
	 * 
	 * @param currPair		The String holding the currency pair the order will deal with
	 * @param buySell		The Object that indicates whether the user wants to buy or sell
	 * 						the currency pair
	 * @param amount		The amount the user wants to trade
	 */
	public void newLO(Object currPair, Object buySell, String amount) {
		hidePanels();
		limitOrder = new LimitOrder(currPair, buySell, amount);
		topPanel.add(limitOrder);
		limitOrder.setVisible(true);
	}
	
	/**
	 * Create a new trailing stop order
	 * 
	 * @param currPair		The String holding the currency pair the user wants to deal with
	 * @param buySell		The Object that indicates whether the user wants to buy or sell
	 * 						the currency pair
	 */
	public void newTS(Object currPair, Object buySell, String amount) {
		hidePanels();
		trailingStop = new TrailingStop(currPair, buySell, amount);
		topPanel.add(trailingStop);
		trailingStop.setVisible(true);
	}
	
	public void popupFail() {
		popupFail("Create Order Failed");
	}
	
	public void popupFail(String msg) {
		JOptionPane.showMessageDialog(this, msg);
	}
	
	public void popupTS(String currPair, String buySell, String amount, String percent) {
		String currs[] = Price.seperateCurrencies(currPair);
		
		if (currs.length == 2) {
			String output = "Creating new trailing stop order " + buySell + "ing " + amount + " of " + currs[1] + " with " + currs[0] + " - Trailing Percent: " + percent;
			JOptionPane.showMessageDialog(this, output);
		}
	}
	
	public void popupNO(String currPair, String buySell, String amount) {
		String currs[] = Price.seperateCurrencies(currPair);
		
		if (currs.length == 2) {
			String output = "Creating new order " + buySell + "ing " + amount + " of " + currs[1] + " with " + currs[0];
			JOptionPane.showMessageDialog(this, output);
		}
	}
	
	public void popupLO(String currPair, String buySell, String amount, String limPrice) {
		String currs[] = Price.seperateCurrencies(currPair);
		
		if (currs.length == 2) {
			String output = "Creating new limit order " + buySell + "ing " + amount + " of " + currs[1] + " with " + currs[0] + " - Limit Price: " + limPrice;
			JOptionPane.showMessageDialog(this, output);
		}
	}
}
